package cn.iocoder.yudao.module.book.controller.app.invite;

import cn.hutool.core.codec.Base64;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.book.controller.app.invite.vo.AppInviteItemRespVO;
import cn.iocoder.yudao.module.book.controller.app.invite.vo.AppInviteMessageRespVO;
import cn.iocoder.yudao.module.book.controller.app.invite.vo.AppInviteProgressRespVO;
import cn.iocoder.yudao.module.system.api.social.SocialClientApi;
import cn.iocoder.yudao.module.system.api.social.dto.SocialWxQrcodeReqDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "APP - 分享邀请")
@RestController
@RequestMapping("/api/user/my")
@Validated
public class AppInviteController {

    @Resource
    private SocialClientApi socialClientApi;

    @GetMapping("/getMyInviteMiniCode")
    @Operation(summary = "获取我的邀请二维码（data url）")
    public CommonResult<String> getMyInviteMiniCode(
            @RequestParam(value = "path", defaultValue = "pages/EnglishBookshelf/index") String path,
            @RequestParam(value = "prod", required = false) Boolean prod
    ) {
        Long userId = SecurityFrameworkUtils.getLoginUserId();
        SocialWxQrcodeReqDTO reqDTO = new SocialWxQrcodeReqDTO();
        reqDTO.setPath(path.replaceFirst("^/", ""));
        reqDTO.setScene("ut=" + userId);
        reqDTO.setWidth(430);
        reqDTO.setAutoColor(true);
        reqDTO.setCheckPath(true);
        reqDTO.setHyaline(false);
        byte[] pngBytes = socialClientApi.getWxaQrcode(reqDTO);
        String base64 = Base64.encode(pngBytes);
        return success("data:image/png;base64," + base64);
    }

    @GetMapping("/myInviteProgress")
    @Operation(summary = "我的邀请进度")
    public CommonResult<AppInviteProgressRespVO> myInviteProgress() {
        // TODO: 这里应查询真实邀请人数与VIP状态，此处先返回示例数据
        int invitedCnt = 3;
        int vipPerInviteDays = 2;
        int targetDays = 10; // 10天目标
        int invitesForTarget = (int) Math.ceil(1.0 * targetDays / vipPerInviteDays); // 5人
        int progress = Math.min(100, invitedCnt * 100 / invitesForTarget);

        int permanentTargetInvites = 50;
        int permanentVipProgress = Math.min(100, invitedCnt * 100 / permanentTargetInvites);

        AppInviteProgressRespVO vo = new AppInviteProgressRespVO();
        vo.setInvitedCnt(invitedCnt);
        vo.setProgress(progress);
        vo.setPermanentVipProgress(permanentVipProgress);
        return success(vo);
    }

    @GetMapping("/permanentVipProgressMessage")
    @Operation(summary = "永久会员进度文案")
    public CommonResult<AppInviteMessageRespVO> permanentVipProgressMessage() {
        // TODO: 真实逻辑应根据最近一次邀请等信息计算
        AppInviteMessageRespVO vo = new AppInviteMessageRespVO();
        vo.setAdd(2); // 最近一次增加的百分比
        vo.setNow(80); // 当前距离100%的差值
        return success(vo);
    }

    @GetMapping("/myInviteList")
    @Operation(summary = "我的邀请列表")
    public CommonResult<List<AppInviteItemRespVO>> myInviteList() {
        // TODO: 返回真实邀请记录
        return success(new ArrayList<>());
    }
}


